package com.zhou.kotlin_car_data_list.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.zhou.kotlin_car_data_list.R
import com.zhou.kotlin_car_data_list.bean.ResultBean
import com.zhou.kotlin_car_data_list.databinding.ItemBrandlistBinding
import com.zhou.kotlin_car_data_list.ui.ModelCarListActivity

/**
 * </br>
 * Date: 2025/8/25
 * Created by:zhourui
 */
class BrandListAdapter(var context: Context, var mData: MutableList<ResultBean>) :
    RecyclerView.Adapter<BrandListAdapter.BrandListHolder>() {

    val inflater = LayoutInflater.from(context)

    class BrandListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding: ItemBrandlistBinding? = null
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BrandListHolder {
        val binding: ItemBrandlistBinding =
            DataBindingUtil.inflate(inflater, R.layout.item_brandlist, parent, false)
        val holder = BrandListHolder(binding.root)
        holder.binding = binding
        return holder
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: BrandListHolder, position: Int) {
        val bean = mData.get(position)
        holder.binding?.brandListItem = bean
        holder.binding?.root?.setOnClickListener(object : View.OnClickListener {
            override fun onClick(p0: View?) {
                context.startActivity(Intent(context, ModelCarListActivity::class.java).apply {
                    putExtra("brandBean", bean)
                })
            }
        })
    }
}